package com.book.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.book.domain.User;
import com.book.util.DBUtil;

public class UserDao {

	public User selectUser (User user){
	Connection connection=	DBUtil.getConnection();
	String sql="select * from t_user where username=? and password=?";
	PreparedStatement preparedStatement=null;
	ResultSet resultSet=null;
	User user2=null;
	try {
		preparedStatement=connection.prepareStatement(sql);
		preparedStatement.setString(1, user.getUsername());
		preparedStatement.setString(2, user.getPassword());
	    resultSet=   preparedStatement.executeQuery();
	    if(resultSet.next()){
	    	user2=new User(resultSet.getInt("id"),resultSet.getString("username"),resultSet.getString("password"));
	    	
	    }else {
			user2=null;
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}	
	return user2;
	}
}
